// miniprogram/pages/employee/employee.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    tasks:[],
    taskchoose:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(app.globalData.user)
    this.setData({
      name:app.globalData.user.name
    })
    var that = this
    const db = wx.cloud.database()
    db.collection('command').where({
      engineer: app.globalData.user.name
    }).get({
      success: function(res){
        // console.log(res.data)
        that.setData({
          tasks:res.data
        })
        wx.stopPullDownRefresh()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得popup组件
    this.popup = this.selectComponent("#popup");
  },


  showPopup(event) {
    var index = event.currentTarget.dataset.index
    console.log(index)
    this.setData({
      taskchoose:this.data.tasks[index]
    })
    this.popup.showPopup();
  },
 
  //取消事件
  _error() {
    console.log('你点击了取消');
    this.popup.hidePopup();
  },
  //确认事件
  _success() {
    const db = wx.cloud.database()
    console.log(this.data.taskchoose);
    var that = this
    db.collection('command').doc(this.data.taskchoose._id).update({
      data: {
        // 表示将 done 字段置为 true
        done: true
      },
      success: function(res) {
        that.onLoad()
      }
    })
    this.popup.hidePopup();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})